/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GradeRuleContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseGradeRule;
import com.xteam.ecsystem.service.admin.GradeRuleService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.GradeRuleUtil;

/**
 * 等级规则控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GradeRuleContorller {

	@Autowired
	private GradeRuleService bgrService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGradeRuleList.do")
	public void getGradeRuleList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		String strList = this.bgrService.getGradeRuleList(pageIndex, pageSize);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGradeRule.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "gradeRuleInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String isAddNew = ReqHelper.getRequestString(request, "isAddNew");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseGradeRule gradeRule = (BaseGradeRule)JSONObject.toBean(obj, BaseGradeRule.class);
		
		if(!Boolean.parseBoolean(isAddNew)){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GRADERULE_MGR, 
				"saveOrUpdateObject", lslType, "BaseGradeRule");
		
		this.bgrService.saveOrUpdateObject(gradeRule, Boolean.parseBoolean(isAddNew));
	}
	
	@RequestMapping("/getGradeRuleById.do")
	public void getGradeRuleById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgrId = ReqHelper.getRequestString(request, "bgrId");		
		String strInfo = this.bgrService.getGradeRuleById(bgrId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateGradeRuleStateById.do")
	public void updateGradeRuleStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgrId = ReqHelper.getRequestString(request, "bgrId");
		String state = ReqHelper.getRequestString(request, "state");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.bgrService.updateGradeRuleStateById(bgrId, Integer.parseInt(state));
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GRADERULE_MGR, 
				"updateGradeRuleStateById", ConstantLocation.LOG_Edit_STATE, "BaseGradeRule");

	}
	
	@RequestMapping("/getBgrTopData.do")
	public void getBgrTopData(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String topRule = this.bgrService.getBgrTopData();
		response.getWriter().write(topRule);
	}
	
	@RequestMapping("/validBgrRuleExp.do")
	public void validBgrRuleExp(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String expText = ReqHelper.getRequestString(request, "expText");
		String upgrade = ReqHelper.getRequestString(request, "upgrade");
		
		if (GradeRuleUtil.validate(expText, Boolean.parseBoolean(upgrade))) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
